package com.hero.tweaks.ui.activities;

import net.margaritov.preference.colorpicker.ColorPickerPreference;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.provider.Settings;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.hero.tweaks.R;
import com.hero.tweaks.util.AxisPreferenceActivity;
import com.hero.tweaks.util.CMDProcessor;
import com.hero.tweaks.util.Helpers;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import com.htc.widget.ActionBarContainer;
import com.htc.widget.ActionBarExt;
import com.htc.widget.ActionBarText;

public class PrismTextColors extends AxisPreferenceActivity implements
		Preference.OnPreferenceChangeListener,
		OnSharedPreferenceChangeListener {

	private static final String PREF_HOMESCREEN_COLOR = "pref_homescreen_color";
	private static final String PREF_DRAWER_COLOR = "pref_drawer_color";
	private static final String PREF_FOLDER_COLOR = "pref_folder_color";
	private static final String PREF_FOLDER_TITLE_COLOR = "pref_folder_title_color";
	//private static final String PREF_ADD_HOME_COLOR = "pref_add_home_color";
	//private static final String PREF_ADD_PANEL_COLOR = "pref_add_panel_color";
	//private static final String PREF_DROP_TARGET_COLOR = "pref_drop_target_color";
	//private static final String PREF_WORKSPACE_CANCEL_COLOR = "pref_workspace_cancel_color";
	//private static final String PREF_WORKSPACE_EDIT_COLOR = "pref_workspace_edit_color";
	

	ColorPickerPreference mHomescreenColor;
	ColorPickerPreference mDrawerColor;
	ColorPickerPreference mFolderColor;
	ColorPickerPreference mFolderTitleColor;
	//ColorPickerPreference mAddHomeColor;
	//ColorPickerPreference mAddPanelColor;
	//ColorPickerPreference mDropTargetColor;
	//ColorPickerPreference mWorkspaceCancelColor;
	//ColorPickerPreference mWorkspaceEditColor;
	

	public void Hreboot() {
		CMDProcessor.runSuCommand("setprop ctl.restart zygote");
	}

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		SetupActionBar();
		overridePendingTransition(R.anim.translate_to_right_in, R.anim.hold_out);

		addPreferencesFromResource(R.xml.prefs_prism_text_colors);

		mHomescreenColor = (ColorPickerPreference) findPreference(PREF_HOMESCREEN_COLOR);
		mHomescreenColor.setOnPreferenceChangeListener(this);
		
		mDrawerColor = (ColorPickerPreference) findPreference(PREF_DRAWER_COLOR);
		mDrawerColor.setOnPreferenceChangeListener(this);
		
		mFolderColor = (ColorPickerPreference) findPreference(PREF_FOLDER_COLOR);
		mFolderColor.setOnPreferenceChangeListener(this);
		
		mFolderTitleColor = (ColorPickerPreference) findPreference(PREF_FOLDER_TITLE_COLOR);
		mFolderTitleColor.setOnPreferenceChangeListener(this);
		
		/*mAddHomeColor = (ColorPickerPreference) findPreference(PREF_ADD_HOME_COLOR);
		mAddHomeColor.setOnPreferenceChangeListener(this);

		mAddPanelColor = (ColorPickerPreference) findPreference(PREF_ADD_PANEL_COLOR);
		mAddPanelColor.setOnPreferenceChangeListener(this);

		mDropTargetColor = (ColorPickerPreference) findPreference(PREF_DROP_TARGET_COLOR);
		mDropTargetColor.setOnPreferenceChangeListener(this);

		mWorkspaceCancelColor = (ColorPickerPreference) findPreference(PREF_WORKSPACE_CANCEL_COLOR);
		mWorkspaceCancelColor.setOnPreferenceChangeListener(this);

		mWorkspaceEditColor = (ColorPickerPreference) findPreference(PREF_WORKSPACE_EDIT_COLOR);
		mWorkspaceEditColor.setOnPreferenceChangeListener(this);*/

	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object objValue) {

		if (preference == mHomescreenColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_label_color", intHex);
			return true;
		} else if (preference == mDrawerColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_drawer_label_color", intHex);
			return true;
		} else if (preference == mFolderColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_folder_color", intHex);
			return true;
		} else if (preference == mFolderTitleColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_folder_title_color", intHex);
			return true;
		/*} else if (preference == mAddHomeColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_add_home_color", intHex);
			return true;
		} else if (preference == mAddPanelColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_add_panel_color", intHex);
			return true;
		} else if (preference == mDropTargetColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_drop_target_color", intHex);
			return true;
		} else if (preference == mWorkspaceCancelColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_workspace_cancel_color", intHex);
			return true;
		} else if (preference == mWorkspaceEditColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
					.valueOf(String.valueOf(objValue)));

			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
					"tweaks_prism_workspace_edit_color", intHex);
			return true;*/
		}
		return false;
	}

	@SuppressWarnings("deprecation")
	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {

		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1) {
		// TODO Auto-generated method stub

	}

	private void SetupActionBar() {
		ActionBarExt localActionBarExt = new ActionBarExt(this, getActionBar());
		localActionBarExt.setFullScreenEnabled(true);
		ActionBarContainer localActionBarContainer = localActionBarExt
				.getCustomContainer();
		mActionText = new ActionBarText(this);
		mActionText.setPrimaryText(getString(R.string.prism_color_title));
		localActionBarContainer.addCenterView(mActionText);
		localActionBarContainer.setBackUpEnabled(true);
		View localView = localActionBarContainer.getChildAt(0);
		if (localView != null)
			localView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View paramAnonymousView) {
					PrismTextColors.this.finish();
				}
			});
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.add("ContextMenu");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		menu.add(1, 1, 1, R.string.restart_sysui);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		item.getMenuInfo();
		switch (item.getItemId()) {
		case 1:
			Helpers.restartSystemUI();
		default:
			return super.onContextItemSelected(item);
		}
	}
}
